import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wangxin
 * Date: 2024-12-27
 * Time: 20:16
 */
public class Graph {
    static class Node{
        public int src;
        public int desc;
        public int weight;
        public Node next;
        public Node(int src, int desc, int weight) {
            this.src = src;
            this.desc = desc;
            this.weight = weight;
        }
    }
    public ArrayList<Node> edgeList;
    public char[] arrayV;
    public boolean isDirect;
    public Graph(int size,boolean isDirect){
        this.arrayV = new char[size];
        edgeList = new ArrayList<>(size);
        for(int i = 0;i < size;i++){
            edgeList.add(null);
        }
        this.isDirect = isDirect;
    }
    public void initArrayV(char[] array){
        for(int i= 0;i < array.length;i++){
            arrayV[i] = array[i];
        }
    }
    public int findArrayVIndex(char v){
        for(int i = 0;i < arrayV.length;i++){
            if(arrayV[i] == v){
                return i;
            }
        }
        return -1;
    }
    public void addEdge(char v1,char v2,int weight){
        int src = findArrayVIndex(v1);
        int desc = findArrayVIndex(v2);
        addEdgeChild(src,desc,weight);
         if(!isDirect){
             addEdgeChild(desc,src,weight);
         }
    }

    private void addEdgeChild(int src, int desc, int weight) {
        Node cur = edgeList.get(src);
        while (cur != null){
            if(cur.desc == desc){
                return;
            }
            cur = cur.next;
        }
        Node node = new Node(src,desc,weight);
        node.next = edgeList.get(src);
        edgeList.set(src,node);
    }
    public int getDevOfV(char v){
        int vIndex = findArrayVIndex(v);
        Node cur = edgeList.get(vIndex);
       int count = 0;
       while (cur != null){
           count++;
           cur = cur.next;
       }
       if(isDirect){
           int desc = vIndex;
           for(int src = 0;src < edgeList.size();src++){
               if(src == desc){
                   continue;
               }
              Node pCur = edgeList.get(desc);
               while(pCur.desc == desc){
                   count++;
                   pCur = pCur.next;
               }
           }

       }
       return count;
    }
    public void printGraph(){
        for(int i = 0;i < arrayV.length;i++){
            Node cur = edgeList.get(i);
          System.out.print(arrayV[i]+"->");
            while(cur != null){
                System.out.print(arrayV[cur.desc]+"->");
                cur = cur.next;
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        Graph graph = new Graph(4,false);
        char[] array = {'A','B','C','D'};
        graph.initArrayV(array);
        graph.addEdge('A','B',1);
        graph.addEdge('A','D',1);
        graph.addEdge('B','A',1);
        graph.addEdge('B','C',1);
        graph.addEdge('C','B',1);
        graph.addEdge('C','D',1);
        graph.addEdge('D','A',1);
        graph.addEdge('D','C',1);
        graph.printGraph();
        System.out.println(graph.getDevOfV('B'));
    }
}
